package com.example.photograph.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.example.photograph.common.utils.DateUtils;
import com.example.photograph.domain.StatisticsEmploymentPlusVo;
import com.example.photograph.domain.StatisticsEmploymentQuery;
import com.example.photograph.domain.StatisticsEmploymentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.photograph.mapper.SysEmploymentMapper;
import com.example.photograph.domain.SysEmployment;
import com.example.photograph.service.ISysEmploymentService;
import com.example.photograph.common.core.text.Convert;

/**
 * 就业信息管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-01
 */
@Service
public class SysEmploymentServiceImpl implements ISysEmploymentService 
{
    @Autowired
    private SysEmploymentMapper sysEmploymentMapper;

    @Override
    public SysEmployment selectSysEmploymentByUserId(Long userId) {
        return sysEmploymentMapper.selectSysEmploymentByUserId(userId);
    }

    /**
     * 查询就业信息管理
     * 
     * @param employmentId 就业信息管理ID
     * @return 就业信息管理
     */
    @Override
    public SysEmployment selectSysEmploymentById(Long employmentId)
    {
        return sysEmploymentMapper.selectSysEmploymentById(employmentId);
    }

    /**
     * 查询就业信息管理列表
     * 
     * @param sysEmployment 就业信息管理
     * @return 就业信息管理
     */
    @Override
    public List<SysEmployment> selectSysEmploymentList(SysEmployment sysEmployment)
    {
        return sysEmploymentMapper.selectSysEmploymentList(sysEmployment);
    }

    @Override
    public List<SysEmployment> selectMySysEmploymentList(SysEmployment sysEmployment) {
        return sysEmploymentMapper.selectMySysEmploymentList(sysEmployment);
    }

    /**
     * 新增就业信息管理
     * 
     * @param sysEmployment 就业信息管理
     * @return 结果
     */
    @Override
    public int insertSysEmployment(SysEmployment sysEmployment)
    {

        SysEmployment oldSysEmployment = this.selectSysEmploymentByUserId(sysEmployment.getUserId());
        if (oldSysEmployment != null){
            sysEmployment.setVersions(oldSysEmployment.getVersions()+1);
        }
        sysEmployment.setCreateTime(DateUtils.getNowDate());
        return sysEmploymentMapper.insertSysEmployment(sysEmployment);
    }

    /**
     * 修改就业信息管理
     * 
     * @param sysEmployment 就业信息管理
     * @return 结果
     */
    @Override
    public int updateSysEmployment(SysEmployment sysEmployment)
    {
        SysEmployment oldSysEmployment = this.selectSysEmploymentByUserId(sysEmployment.getUserId());
        if (oldSysEmployment != null){
            sysEmployment.setVersions(oldSysEmployment.getVersions()+1);
        }
        sysEmployment.setCreateTime(DateUtils.getNowDate());
        return sysEmploymentMapper.insertSysEmployment(sysEmployment);
    }

    /**
     * 删除就业信息管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysEmploymentByIds(String ids)
    {
        return sysEmploymentMapper.deleteSysEmploymentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除就业信息管理信息
     * 
     * @param employmentId 就业信息管理ID
     * @return 结果
     */
    @Override
    public int deleteSysEmploymentById(Long employmentId)
    {
        return sysEmploymentMapper.deleteSysEmploymentById(employmentId);
    }

    @Override
    public StatisticsEmploymentPlusVo statisticsEmployment(StatisticsEmploymentQuery statisticsEmploymentQuery) {
        StatisticsEmploymentPlusVo  statisticsEmploymentPlusVo = new StatisticsEmploymentPlusVo();

        statisticsEmploymentPlusVo.setStatisticsEmploymentVos(sysEmploymentMapper.statisticsEmployment(statisticsEmploymentQuery));
        Map map = this.selectMaxMajor(statisticsEmploymentQuery);
        String message ="就业率最高的专业为:" +map.get("majorName")+"\r\n";
        message= message+"就业率为:"+map.get("employmentRate")+"\r\n";
        message= message+"地区分布:";
        Long userId =(Long) map.get("userId");
        boolean firstFlag =true;
        List<String> addressList = sysEmploymentMapper.selectAddress((String) (map.get("majorId")));
        for (String address : addressList) {
            if (!firstFlag){
                message =message+",";
            }
            message =message+address;
            firstFlag=false;
        };

        statisticsEmploymentPlusVo.setMessage(message);

        return statisticsEmploymentPlusVo;
    }

    private Set<String> findEmploymentAddress(Long userId){
        SysEmployment sysEmployment = new SysEmployment();
        sysEmployment.setUserId(Long.valueOf(userId));
        List<SysEmployment> sysEmployments = sysEmploymentMapper.selectMySysEmploymentList(sysEmployment);
        if (sysEmployments.size()>0){

            Set<String> collect = sysEmployments.stream().map(p -> p.getAddress()).collect(Collectors.toSet());
            return collect;
        }else {
            return new HashSet<>();
        }



    }
    /**
     *统计就业率最高的专业
     **/
    @Override
    public Map selectMaxMajor(StatisticsEmploymentQuery statisticsEmploymentQuery) {
        return sysEmploymentMapper.selectMaxMajor(statisticsEmploymentQuery);
    }


}
